package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.model.TSysUserOrg;
import com.blt.govdata.service.ITSysUserOrgService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 用户分配部门管理前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tSysUserOrg")
//todo 将所有的XXX修改为真实值
public class TSysUserOrgController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITSysUserOrgService service;

    /**
     * 给用户配置部门(1对1的关系)
     */
    @RequestMapping( value = "/confUserOrg",params = {"uId"})
    public Map<String, Object> confUserOrg(Integer uId,Integer oId) {
        /**
         * 先删除用户原来配置的部门
         * */
        this.service.delete(BeanUtils.instantiate(EntityWrapper.class).where("u_id = {0}",uId));
        if(null == oId) return ImmutableMap.of("status", "true", "data", true);
        TSysUserOrg suo = BeanUtils.instantiate(TSysUserOrg.class);
        suo.setoId(oId);
        suo.setuId(uId);
        /**
         * 插入新配置的部门
         * */
        boolean result = this.service.insert(suo);
        logger.info("给用户配置部门");
        return ImmutableMap.of("status", "true", "data", result);
    }


}
